<?php

namespace app\api\controller\ytjk;

use app\api\controller\Base;
use \think\facade\Db;

class Data extends Base
{
    protected $answerList = [
        '1' => ['A'],
        '2' => ['B'],
        '3' => ['C'],
        '4' => ['D'],
        '7' => ['A', 'B'],
        '8' => ['A', 'C'],
        '9' => ['A', 'D'],
        '10' => ['B', 'C'],
        '11' => ['B', 'D'],
        '12' => ['C', 'D'],
        '13' => ['A', 'B', 'C'],
        '14' => ['A', 'B', 'D'],
        '15' => ['A', 'C', 'D'],
        '16' => ['B', 'C', 'D'],
        '17' => ['A', 'B', 'C', 'D']
    ];

    protected $total = [
        'c1' => 1323,
        'a1' => 3101,
        'a2' => 3109,
        'd' => 400,
        '4' => 1236
    ];
    protected $table = 'ytjk_question';

    //随机列表
    public function rand()
    {
        $model = input('model', 0);
        $subject = input('subject', 0);
        if (empty($model) || empty($subject)) {
            $this->error('invalid param');
        }
        $total = $this->total;
        if ($subject == '4') {
            $where = [['subject', '=', $subject]];
            $totalNum = $total[$subject];
        } else {
            $where = [['model', '=', $model], ['subject', '=', $subject]];
            $totalNum = $total[$model];
        }
        $pageSize = 20;
        $totalPage = ceil($totalNum / $pageSize);
        $page = mt_rand(1, $totalPage) - 1;
        if ($subject == 4) {
            $cacheKey = 'd4_' . $page;
        } else {
            $cacheKey = $model . '_' . $page;
        }
        $path = './data/ytjk/rand/' . $cacheKey . ".html";
        if (file_exists($path)) {
            $listString = file_get_contents($path);
            $list = json_decode($listString, true);
        } else {
            $list = Db::table($this->table)->where($where)->limit($page * $pageSize, $pageSize)->select()->toArray();
            $answerList = $this->answerList;
            foreach ($list as &$value) {
                $value['answerArr'] = $answerList[$value['answer']];
            }
            if(!empty($list)){
                file_put_contents($path, json_encode($list));
            }
        }

        $this->success("success", $list);

    }

    /**
     * 顺序练习题库
     * @return void
     */
    public function sort_qt()
    {
        $model = input('model', 0);
        $subject = input('subject', 0);
        if (empty($model) || empty($subject)) {
            $this->error('invalid param');
        }
        if ($subject == '4') {
            $where = [['subject', '=', $subject]];
        } else {
            $where = [['model', '=', $model], ['subject', '=', $subject]];
        }
        $pageSize = 20;
        $page = input('page', 1);
        $page = $page - 1;
        $page = (int)$page < 0 ? 0 : $page;
        if ($subject == 4) {
            $cacheKey = 'd4_' . $page;
        } else {
            $cacheKey = $model . '_' . $page;
        }
        $path = './data/ytjk/rand/' . $cacheKey . ".html";
        if (file_exists($path)) {
            $listString = file_get_contents($path);
            $list = json_decode($listString, true);
        } else {
            $list = Db::table($this->table)->where($where)->limit($page * $pageSize, $pageSize)->select()->toArray();
            $answerList = $this->answerList;
            foreach ($list as &$value) {
                $value['answerArr'] = $answerList[$value['answer']];
            }
            if(!empty($list)){
                file_put_contents($path, json_encode($list));
            }
        }

        $this->success("success", $list);
    }

    /**
     * 获取答题卡列表
     * @return void
     */
    public function examine(){
        $model = input('model', 0);
        $subject = input('subject', 0);
        if (empty($model) || empty($subject)) {
            $this->error('invalid param');
        }
        $page = mt_rand(1,100);
        if ($subject == 4) {
            $cacheKey = 'd4_' . $page;
        } else {
            $cacheKey = $model . '_' . $page;
        }
        $path = './data/ytjk/examine/' . $cacheKey . ".html";
        if (file_exists($path)) {
            $listString = file_get_contents($path);
            $list = json_decode($listString, true);
        }else{
            if ($subject == '4') {
                $where = [['subject', '=', $subject]];
            } else {
                $where = [['model', '=', $model], ['subject', '=', $subject]];
            }
            $list = Db::table($this->table)->where($where)->orderRaw("RAND()")->limit(100)->select()->toArray();
            $answerList = $this->answerList;
            foreach ($list as &$value) {
                $value['answerArr'] = $answerList[$value['answer']];
            }
            if(!empty($list)){
                file_put_contents($path, json_encode($list));
            }
        }
        $this->success("success", $list);

    }

}
